package nl.windgazer.arachne.components.socialmedia.facebook;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;

import nl.windgazer.arachne.internal.URLHelper;
import nl.windgazer.arachne.services.facebook.FacebookShareLinkType;

import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.annotations.Environmental;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.PageRenderLinkSource;
import org.apache.tapestry5.services.javascript.JavaScriptSupport;

/**
 * This component allows one to easily add a Facebook 'share' link to a website. In order for this
 * link to work properly one must also use the @IncludeFaceBookOpenGraphInfo annotation to provide
 * the required information to the facebook sharer.php scripts.<br />
 * Facebook recommends using the 'like' button functionallity instead of the share-link, another
 * component exists in this package to provide that functionallity.
 * 
 * @author mreuring
 *
 */
public class FacebookShareLink {

	//Injects and Persists

	@Environmental
	private JavaScriptSupport _js;

	@Inject
	private HttpServletRequest _request;

	@Inject
	private ComponentResources _cr;

	@Inject
	private PageRenderLinkSource _prls;

	//Properties and Parameters

	@Parameter
	private String url;

	@Parameter
	private String title;

	@SuppressWarnings("unused")
	@Parameter
	@Property
	private FacebookShareLinkType type = FacebookShareLinkType.button_count;

	/**
	 * If the link is supposed to be styled in a custom manner and the content is provided,
	 * setting this property to true will prevent it from being modified.
	 */
	@Parameter
	private boolean custom = false;

	private StringBuilder query = new StringBuilder();

	//Components

	//Events

	@SetupRender
	void setupMetaTags() throws UnsupportedEncodingException {

		if (url==null) {
			url = URLHelper.getFullURL(_prls.createPageRenderLink(_cr.getPageName()).toAbsoluteURI(), _request);
		}
		buildQuery();

		// Add link to FB-share script @ http://static.ak.fbcdn.net/connect.php/js/FB.Share
		if (!custom) {
			_js.importJavaScriptLibrary("http://static.ak.fbcdn.net/connect.php/js/FB.Share");
		}

	}

	//Other (check for business-logic, it should not remain here)

	private void buildQuery() throws UnsupportedEncodingException {
		if (url!=null) {
			addQuery("u",url);
		}

		if (title!=null) {
			addQuery("t", title);
		}
	}

	private void addQuery(String key, String value) throws UnsupportedEncodingException {
		if (query.length() == 0) {
			query.append('?');
		} else {
			query.append('&');
		}
		query.append(URLEncoder.encode(key, "UTF-8"));
		query.append('=');
		query.append(URLEncoder.encode(value, "UTF-8"));
	}

	public String getQuery() throws UnsupportedEncodingException {
		return query.toString();
	}

	public String getName() {
		if (custom) {
			return "custom_share";
		} else {
			return "fb_share";
		}
	}
}
